using System;
using UnityEngine;
using System.IO;
using System.Text;

public static class Tools
{
    public static double ConvertDateTimeInt(DateTime time)
    {
        DateTime dd = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        TimeSpan ts = (time - dd);
        return (long)ts.TotalMilliseconds;
    }

    public static DateTime ConvertStringToDateTime(string timeStamp)
    {
        DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
        long lTime = long.Parse(timeStamp + "0000");
        TimeSpan toNow = new TimeSpan(lTime);
        return dtStart.Add(toNow);
    }
    /// <summary>
    /// 获取平台类型
    /// </summary>
    /// <returns></returns>
    public static string GetPlatformName()
    {
        string platformName = "";
#if UNITY_IPHONE
        platformName = "iOS";
#elif UNITY_ANDROID
        platformName = "Android";
#elif UNITY_EDITOR_WIN
        platformName = "StandaloneWindows";
#else
        platformName = "StandaloneWindows";
#endif
        return platformName;
    }
    /// <summary>
    /// 16位颜色转换为UnityColor 颜色
    /// </summary>
    /// <param name="color"></param>
    /// <returns></returns>
    public static Color GetColor(string color)
    {
        if (color.Length == 0)
        {
            return Color.black;//设为黑色
        }
        else
        {
            //#ff8c3 除掉#
            // color = color.Substring(1);
            int v = int.Parse(color, System.Globalization.NumberStyles.HexNumber);
            //转换颜色
            return new Color(
                //int>>移位 去低位
                //&按位与 去高位
                ((float)(((v >> 16) & 255))) / 255f,
                ((float)((v >> 8) & 255)) / 255f,
                ((float)((v >> 0) & 255)) / 255f
            );
        }
    }

    static public bool IsToday(long stamp)
    {
        return IsToday(stamp.ToString());
    }
    static public bool IsToday(int stamp)
    {
        return IsToday(stamp.ToString());
    }
    static public bool IsToday(string stamp)
    {
        if (stamp.Length > 10)
            stamp = stamp.Substring(0, 10);
        DateTime dateTimeStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
        long lTime = long.Parse(stamp + "0000000");
        TimeSpan toNow = new TimeSpan(lTime);
        DateTime dt = dateTimeStart.Add(toNow);
        return dt.DayOfYear == DateTime.Now.DayOfYear;
    }

    /// <summary>
    /// 当前时间戳（毫秒）
    /// </summary>
    static public long GetCurrentTimeStamp_Long()
    {
        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
        return Convert.ToInt64(ts.TotalMilliseconds);
    }
    /// <summary>
    /// 当前时间戳（秒）
    /// </summary>
    static public int GetCurrentTimeStamp_Int()
    {
        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
        return (int)(Convert.ToInt64(ts.TotalMilliseconds) / 1000);
    }

    /// <summary>
    /// 时间转换
    /// </summary>
    /// <param name="type">  1-XX:XX  2-XX:XX:XX 3-XX天 </param>
    /// <param name="time"></param>
    /// <returns></returns>
    static public string ConvertTime(int type, float time)
    {
        if (time <= 0)
        {
            switch (type)
            {
                case 1:
                    return "00:00";
                case 2:
                case 3:
                    return "00:00:00";
                case 4:
                    return "0s";
            }
        }

        string str = "";
        switch (type)
        {
            case 1:
                {
                    int minute = Mathf.FloorToInt(time / 60f);
                    int second = Mathf.FloorToInt(time % 60);
                    string mStr = minute < 10 ? "0" + minute : minute.ToString();
                    string sStr = second < 10 ? "0" + second : second.ToString();
                    str = mStr + ":" + sStr;
                }
                break;
            case 2:
                {
                    int hour = Mathf.FloorToInt(time / 3600f);
                    int minute = Mathf.FloorToInt((time % 3600) / 60f);
                    int second = Mathf.FloorToInt(time % 60);
                    string hStr = hour < 10 ? "0" + hour : hour.ToString();
                    string mStr = minute < 10 ? "0" + minute : minute.ToString();
                    string sStr = second < 10 ? "0" + second : second.ToString();
                    str = hStr + ":" + mStr + ":" + sStr;
                }
                break;
            case 3:
                {
                    if (time > 86400)
                    {
                        str = (int)(time / 86400) + "天";
                    }
                    else
                    {
                        int hour = Mathf.FloorToInt(time / 3600f);
                        int minute = Mathf.FloorToInt((time % 3600) / 60f);
                        int second = Mathf.FloorToInt(time % 60);
                        string hStr = hour < 10 ? "0" + hour : hour.ToString();
                        string mStr = minute < 10 ? "0" + minute : minute.ToString();
                        string sStr = second < 10 ? "0" + second : second.ToString();
                        str = hStr + ":" + mStr + ":" + sStr;
                    }
                }
                break;
            case 4:
                {
                    int hour = Mathf.FloorToInt(time / 3600f);
                    int minute = Mathf.FloorToInt((time % 3600) / 60f);
                    int second = Mathf.FloorToInt(time % 60);
                    string hStr = hour > 0 ? hour + "h" : "";
                    string mStr = minute > 0 ? minute + "m" : "";
                    string sStr = second > 0 ? second + "s" : "0s";
                    str = hStr + mStr + sStr;
                }
                break;
        }

        return str;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="total"></param>
    /// <param name="count"></param>
    /// <returns></returns>
    static public int[] GetRandomSequence(int total, int count)
    {
        int[] sequence = new int[total];
        int[] output = null;
        if (count <= total)
        {
            output = new int[count];
            for (int i = 0; i < total; i++)
            {
                sequence[i] = i;
            }
            int end = total - 1;
            for (int i = 0; i < count; i++)
            {
                //随机一个数，每随机一次，随机区间-1
                int num = UnityEngine.Random.Range(0, end + 1);
                output[i] = sequence[num];
                //将区间最后一个数赋值到取到的数上
                sequence[num] = sequence[end];
                end--;
            }
        }
        else
        {
            output = new int[total];
            for (int i = 0; i < total; i++)
            {
                output[i] = i;
            }
        }
        return output;
    }

}